import os,json
from PIL import Image
def cropped_main():
    f = open('/home/ubuntu/data/multi_label/data/feature_images.txt','r').readlines()
    dict = {}
    for file in os.listdir(r'/home/ubuntu/data_tongue'):
        if file.startswith('inquiry'):
            for i in os.listdir(r'/home/ubuntu/data_tongue/'+file):
                dict[i] = os.path.join(r'/home/ubuntu/data_tongue',file,i)
    for line in f:
        line_list = line.strip('\n').split(',')
        image,detec_box = line_list[0],line_list[1:]
        if not os.path.exists('/home/ubuntu/data_tongue/cropp_pictures/' + image):
                img = Image.open(dict[image])
                print(img)
                # 图片尺寸
                img_size = img.size
                h = img_size[1]  # 图片高度
                w = img_size[0]  # 图片宽度
                try:
                    xmin = float(detec_box[0]) * w/416.
                    ymin = float(detec_box[1]) * h/416.
                    xmax = float(detec_box[2]) * w/416.
                    ymax = float(detec_box[3]) * h/416.
                    crop_img = img.crop((xmin, ymin, xmax, ymax))

                    crop_img.save('/home/ubuntu/data_tongue/cropp_pictures/' + image)
                except (Exception):
                    print('string bug')
                # 开始截取


def main():
    # f = open('/home/ubuntu/data/multi_label/data/feature_images.txt', 'r').readlines()
    dict = {}
    for file in os.listdir(r'/home/ubuntu/data_tongue'):
        if not file.startswith('inquiry'):
            path = r'/home/ubuntu/data_tongue/'+file
            if file.startswith('source'):
                path = r'/home/ubuntu/data_tongue/'+file+'/top/'
            for i in os.listdir(path):
                img_id = i.split('!')[0][8:]
                dict[img_id] = os.path.join(path, i)
    # data_inquiry = r'/home/ubuntu/data/multi_label/test/inquiry/data.txt'
    # f = open(data_inquiry, 'w')
    num_c = 5
    num_class = 2
    quetion_path = r'/home/ubuntu/data/multi_label/test/inquiry/inquiry_questions/'+str(num_c)+'.txt'
    data_f = open(quetion_path, 'r').readlines()
    count = 0
    count_normal = 0
    count_one = 0
    count_two = 0
    count_three = 0
    train_path = '/home/ubuntu/data/dataset/train_question'+str(num_c)
    test_path = '/home/ubuntu/data/dataset/test_question' + str(num_c)
    if not os.path.exists(train_path):
        os.mkdir(train_path)
        for i in range(num_class):
            os.mkdir(os.path.join(train_path+'/'+str(i)))

    if not os.path.exists(test_path):
        os.mkdir(test_path)
        for i in range(num_class):
            os.mkdir(os.path.join(test_path+'/'+str(i)))
    for quetion in data_f:

        quetion_list =quetion.strip('\n').strip('[|]').split(', [')
        user_id = quetion_list[0].strip("'").split(', ')[0].strip("'")
        len_img = quetion_list[0].strip("'").split(', ')[1:]
        labels = [int(i) for i in quetion_list[-1].split(', ')]
        for img in len_img:
            image_name = img.strip("'").split('/')[-1].split('!')[0][8:]
            if image_name in dict.keys() and sum(labels) ==1:
                if labels[-1] == 1 and sum(labels[:-1])>0 :
                    break
                import shutil
                if labels[1] == 1:
                    num = 1
                else:
                    num = 0
                count += 1
                print(count)
                if count % 9 !=0:
                    path = train_path
                else:
                    path = test_path
                    if not os.path.exists(path+'/'+str(num)+'/'+image_name+'.jpg'):
                        shutil.copy(dict[image_name],path+'/'+str(num)+'/'+image_name+'.jpg')
                # else:
                # if labels[-1] ==1:
                #     count_normal += 1
                # elif sum(labels) == 1:
                #     count_one += 1
                # elif sum(labels) == 2:
                #     count_two += 1
                # elif sum(labels) == 3:
                #     count_three += 1
                # count += 1
                #
                # data_dict = {
                #     "image_id": image_name,
                #     "image_file": dict[image_name],
                #     "id":[str(i) for i in labels]
                # }
                # print(count,data_dict)
                #
                # f.write(json.dumps(data_dict)+'\n')

                    # break
    print(count_normal,count_one,count_two)


if __name__ == '__main__':
    main()